export default class common {
  constructor() {
    const Vue_ = arguments[0].prototype;
    Vue_.$removeLocalStorage = this.removeLocalStorage;
    Vue_.$Timestamp = this.Timestamp;
    Vue_.$getWeek = this.getWeek;
    Vue_.$sort = this.sort;
    Vue_.$Distinct = this.Distinct;
    Vue_.$isEmptyObject = this.isEmptyObject;
    Vue_.$validator = this.validator;
    Vue_.$copy      = this.copy;
    Vue_.$import_excal=this.import_excal;
    Vue_.$toU       = this.toU;
    Vue_.$toL       = this.toL;
    Vue_.$splitK    = this.splitK;
  }
  /**
   *  清除缓存
   *  @param   {Array}    array       数组    列如  ['删除key名']
   *  @param   {String}   array      字符串   列如  指定删除key
   *  如果不传就是全部删除
   * 
   */
  removeLocalStorage(parameter) {
    if (parameter && Array.isArray(parameter)) {
      parameter.forEach((v, k) => {
        localStorage.removeItem(v);
      })
    } else
      if (parameter && typeof parameter == "string") {
        localStorage.removeItem(parameter);
      } else {
        localStorage.clear();
      }
  }
  /**
   *  时间戳转年月日时分秒
   *  @param   {Number}   stamp     stamp参数  字符串或者number类型时间戳10位或者13位长度
   *   
   */
  Timestamp(stamp) {
    // 判断是不是13位的时间戳格式
    if (stamp.length < 13) {
      stamp = stamp * 1000
    } else {
      stamp = stamp
    }
    let date = new Date(Number(stamp)),
      year = date.getFullYear(),
      month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0' + (date.getMonth() + 1),
      day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate(),
      hour = date.getHours() > 9 ? date.getHours() : '0' + date.getHours(),
      minute = date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(),
      second = date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds();

    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  }


  /**
   *  获取当前时间星期几
   *  @param {Time}   date    例如 2018-11-8
   * 
   */
  getWeek(date) {
    return "星期" + "日一二三四五六".charAt(new Date(date).getDay());
  }

  /**
   *   @method
   *   @param {Array}  arrays
   *   arrays  分别为: [字段名，0升序  1降序  【字段比大小】]
   */
  sort(...arrays) {
    return function (a, b) {
      var value1 = a[arrays[0]];
      var value2 = b[arrays[0]];
      if (arrays[1] == 1) {
        return value2 - value1;
      } else {
        return value1 - value2;
      }
    }
  }
  /**
   *   @method
   *   @param {Array}  arrays
   *   arrays  分别为: [数组:[{}]，字段名称]
   */
  Distinct(...arrays) {
    var hash = {};
    return arrays[0].reduce(function (item, next) {
      hash[next[arrays[1]]] ? '' : hash[next[arrays[1]]] = true && item.push(next);
      return item;
    }, []);
  }
  /**
  *   @method
  *   @param {Object}  {}
  *  
  */
  isEmptyObject(obj) {
    console.log("obj", obj)
  }

  /**
   *   @method
   *   调用方法 返回验证结果 true|false
   */
  validator() {
    return {
      /**
       * 判断是否是手机号码
       */
      isMobile: function (str) {
        return /^(13|14|15|16|17|18|19)\d{9}$/i.test(str);
      },

      /**
       * 判断是否是电话号码
       */
      isTel: function (str) {
        return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(str);
      },

      /**
       * 判断是否是邮箱地址
       */
      isEmail: function (str) {
        return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(str);
      },

      /**
       * 判断是否是邮编
       */
      isZip: function (str) {
        return /^[\d]{6}$/.test(str);
      },

      /**
       * 判断是否是中文
       */
      isChinese: function (str) {
        return /^[\u4e00-\u9fa5]+$/i.test(str);
      },

      /**
       * 判断是否是英文
       */
      isEnglish: function (str) {
        return /^[A-Za-z]+$/i.test(str);
      },
      /**
       * 判断是否是数字
       */
      isCumberNo: function (str) {
        console.log(str)
        return /^[0-9]+$/.test(str)
      },

      /**
       * 判断是否是发动机号
       */
      isCarEngineNo: function (str) {
        return /^[a-zA-Z0-9]{16}$/.test(str);
      },
      /**
       * 判断是否是身份证
       */
      isCard: function (str) {
        return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str)
      },
      /**
       *  只取二位小数点
       */
      towDecimal: function (str) {
        let $value;
        let $values = str
        $value = $values.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符  
        $value = $values.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的  
        $value = $values.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        $value = $values.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数  
        if ($values.indexOf(".") < 0 && $values != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
          $value = parseFloat($values);
        }
        return $value;
      },
      /**
       *  根据字节长度判断
       */
      isByteLength: function (str) {
        return str.length
      },
      /**
       * 判断是不是非法字符
       */
      isIllegalCharacter: function (str) {
        return RegExp(/[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;) (\:) (\')(\`)(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/).test(str)
      },
      /**
       * 提取字符串或html标签里面的url
       */
      isHttp: function (str) {
        var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
        //var reg = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
        //var reg=/(http(s)?\:\/\/)?(www\.)?(\w+\:\d+)?(\/\w+)+\.(swf|gif|jpg|bmp|jpeg)/gi;
        var reg = /(http(s)?\:\/\/)?(www\.)?(\w+\:\d+)?(\/\w+)+\.(swf|gif|jpg|bmp|jpeg)/gi;
        var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
        //var reg= /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
        //v = v.replace(reg, "<a href='$1$2'>$1$2</a>"); //这里的reg就是上面的正则表达式
        //s = s.replace(reg, "$1$2"); //这里的reg就是上面的正则表达式
        var s = str.match(reg);
        return reg.test(str)
      },
      /**
       * 富文本字体大小修改
       */
      isPxToRem(str) {
        //匹配:20px或: 20px不区分大小写
        var reg = /(\:|: )+(\d)+(px)/gi;
        let newStr = str.replace(reg, function (_x) {
          _x = _x.replace(/(\:|: )/, '').replace(/px/i, '');
          return ':' + 0 + 'rpx';
        });
        return newStr;
      },
      /**
       * 千位符
       */
      toThousands(num) {
        return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
      }
    };
  }
  /**
   *  @method
   * 
   */
  copy(data) {
    let data_ = data||'', oInput = document.createElement('input');
    console.log("传入参数",data_)
    oInput.value = data_;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象;
    console.log(oInput.value)
    document.execCommand("Copy"); // 执行浏览器复制命令
    this.$message({
      message: '复制成功',
      type: 'success'
    });
    oInput.remove()
  }

  /**
   * 
   *   批量导入
   * 
   */
  import_excal(url){
        if(url){
             window.location.href=url||'';
        }else{
             this.$message.error("暂无数据");
        }
  }
  /**
   * 
   *   转大写字母
   * 
   */
  toU(str){
      return  str?str.toUpperCase():''
  }

 /**
   * 
   *   转大写字母
   * 
   */
  toL(){
      return  str?str.toLowerCase():''
  }


   /**
   * 金额转为千分符
   */
  splitK(num){
    let decimal = String(num).split('.')[1] || '';//小数部分
    let tempArr = [];
    let revNumArr = String(num).split('.')[0].split("").reverse();//倒序
    for (let i in revNumArr){
      tempArr.push(revNumArr[i]);
      if((i+1)%3 === 0 && i != revNumArr.length-1){
        tempArr.push(',');
      }
    }
    let zs = tempArr.reverse().join('');//整数部分
       zs = zs.replace('-,','-');
    return decimal?zs+'.'+decimal:zs;
  }

}